package com.yuandian.customer.perception.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.BasePortraitParams;
import com.yuandian.api.customer_perception.vo.*;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.BasePortraitMapper;
import com.yuandian.customer.perception.service.IBasePortraitService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.utils.PageDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author liXin
 * @description 单位画像
 * @date 2022/4/24
 */
@Service
public class BasePortraitServiceImpl implements IBasePortraitService {

    @Autowired
	BasePortraitMapper basePortraitMapper;

    /**
     * 办理事项统计  默认按照成功率降序排序
     */
    @Override
    public R<Object> queryMatterProcessStatistics(BasePortraitParams params) {
        // 默认按照成功率降序排序
        params.setOrderByColumn(StringUtils.isBlank(params.getOrderByColumn()) ? "successRate" : params.getOrderByColumn());
        params.setIsAsc(StringUtils.isBlank(params.getOrderByColumn()) ? "asc" : params.getIsAsc());
        Page<MatterProcessStatisticsVO> page = new Page<>(params.getPageNum(), params.getPageSize());

		//查询数据
		IPage<MatterProcessStatisticsVO> list = basePortraitMapper.queryMatterProcessStatistics(page, params, TableNameEnum.BUSI_ITEM_CHANGE.getName());

        // 分页
        return R.ok(new PageDataInfo(list));
    }

    /**
     * 办理事项明细
     */
    @Override
    public R<Object> queryMatterProcessDetail(BasePortraitParams params) {
        Page<MatterProcessDetailVO> page = new Page<>(params.getPageNum(), params.getPageSize());
        IPage<MatterProcessDetailVO> list = basePortraitMapper.queryMatterProcessDetail(page, params, TableNameEnum.BUSI_ITEM_CHANGE.getName());
        return R.ok(new PageDataInfo(list));
    }

    /**
     * 办理事项步骤明细
     */
    @Override
    public R<Object> queryMatterProcessStepDetail(BasePortraitParams params) {

	 	Page<UserOpertorStepOverrideVO> page = new Page<>(params.getPageNum(), params.getPageSize());
		IPage<MatterProcessStepDetailVO> list = basePortraitMapper.queryMatterProcessStepDetail(page, params, Collections.singletonList(TableNameEnum.BUSI_STEP_AFFAIR_M.getName()));

        return R.ok(new PageDataInfo(list));
    }

    /**
     * 用户操作步骤概况
     */
    @Override
    public R<Object> queryUserOperationStepOverview(BasePortraitParams params) {
		// 构建分页
        Page<UserOpertorStepOverrideVO> page = new Page<>(params.getPageNum(), params.getPageSize());

		// 获取表名
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();
        List<String> tableList =  Collections.singletonList(TableNameEnum.BUSI_STEP_AFFAIR_M.getName());

		// 查询数据
        IPage<UserOpertorStepOverrideVO> list = basePortraitMapper.queryUserOperationStepOverview(page, params, tableList, itemChangeTable );

        return R.ok(new PageDataInfo(list));
    }

    /**
     * 事项办理概览  默认按照成功率降序排序
     */
    @Override
    public R<Object> queryPortraitOverview(BasePortraitParams params) {

        params.setOrderByColumn(StringUtils.isBlank(params.getOrderByColumn()) ? "successRate" : params.getOrderByColumn());
        params.setIsAsc(StringUtils.isBlank(params.getOrderByColumn()) ? "asc" : params.getIsAsc());

        Page<MatterProcessOverrideVO> page = new Page<>(params.getPageNum(), params.getPageSize());

        //事项变化表
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();

        //分页
        IPage<MatterProcessOverrideVO> list = basePortraitMapper.queryPortraitOverview(page, params, itemChangeTable);

        return R.ok(new PageDataInfo(list));
    }


    /**
     * 获取事项名称
     */
    @Override
    public R<Object> queryMatterName(BasePortraitParams params) {
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();
        List<MatterInfoVO> matterInfos = basePortraitMapper.queryMatterName(params, itemChangeTable);

        return R.ok(matterInfos);
    }

	/**
	 * 查询已经被告警的步骤id
	 */
    @Override
    public R<Object> queryStepIds(BasePortraitParams params) {
        List<String> list = basePortraitMapper.queryStepIds(params);
        List<String> resultList = new ArrayList<>();
        list.forEach(s -> {
			resultList.addAll(Arrays.asList(s.split(",")));
        });
        return R.ok(resultList);
    }
}
